package com.itedge.solutionmanager.web.taskhandler.impl.solution;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

import com.itedge.solutionmanager.domain.impl.Employee;
import com.itedge.solutionmanager.domain.impl.Solution;
import com.itedge.solutionmanager.taskdata.impl.solution.ScheduleMeetingTaskData;
import com.itedge.solutionmanager.util.StringUtil;
import com.itedge.solutionmanager.web.convert.ISOFormattingConversionServiceBean;
import com.itedge.infrastructure.web.taskhandler.TaskHandlerException;
import com.itedge.infrastructure.web.taskhandler.TaskHandlerUtil;
import com.itedge.infrastructure.web.taskhandler.impl.AbstractTaskDataHandler;
import com.itedge.infrastructure.service.domain.IEntityService;
import com.itedge.infrastructure.service.process.IProcessService;
import com.itedge.infrastructure.service.task.ITaskService;
import com.itedge.infrastructure.service.taskdata.ITaskDataEntityService;
import com.itedge.infrastructure.stereotype.WebHandler;

@WebHandler
public class ScheduleMeetingTaskDataHandler extends AbstractTaskDataHandler<ScheduleMeetingTaskData, Solution> {

    private IEntityService<Employee> employeeService;
	
	@Autowired
    protected ScheduleMeetingTaskDataHandler(ISOFormattingConversionServiceBean conversionService,
    		IProcessService<Solution> solutionProcessService,
    		ITaskDataEntityService<ScheduleMeetingTaskData> scheduleMeetingTaskDataService,
    		ITaskService<ScheduleMeetingTaskData, Solution> scheduleMeetingTaskService,
			MessageSource messageSource, IEntityService<Employee> employeeService) {
		super(conversionService, solutionProcessService, scheduleMeetingTaskDataService, 
				scheduleMeetingTaskService, messageSource);
		this.employeeService = employeeService;
	}
    
	@Override
	protected Class<ScheduleMeetingTaskData> getTaskDataClass() {
		return ScheduleMeetingTaskData.class;
	}
    
	@Override
	public Map<String,Object> loadTaskData(String taskId, Solution linkedSolution, Boolean forHistory) 
			throws TaskHandlerException {
		Map<String,Object> data = super.loadTaskData(taskId, linkedSolution, forHistory);
    	TaskHandlerUtil.applyDateTimePattern(data);
    	data.put("employees", employeeService.findAllEntities());
    	return data;
	}
	
	@Override
	public ScheduleMeetingTaskData bindTaskData(HttpServletRequest request, Solution linkedSolution)
			throws TaskHandlerException {
		ScheduleMeetingTaskData taskData = super.bindTaskData(request, linkedSolution);
		bindEmployee(taskData, request);
		return taskData;
	}	
	
	private void bindEmployee(ScheduleMeetingTaskData taskData, HttpServletRequest request) {
		Set<Employee> emplyees = new HashSet<Employee>();
		String peopleStr = request.getParameter("assignedEmployees");
		if (!StringUtil.isEmpty(peopleStr)) {
			String[] emplyeesIds = peopleStr.split(",");
			for(String temp: emplyeesIds) {
				Long id = new Long(temp);
				Employee p = employeeService.findEntity(id);
				emplyees.add(p);
			}
		}
		taskData.setAssignedEmployees(emplyees);		
	}
	
}
